Scripting Tutorial: Statements



mIRC has a number of common statements found in other programming languages. The use and the logic is the same. They are scattered all over in the mIRC Help File so I have composed them here in a collection so that you won't need to search for them in the Help File and maybe even miss some of them. But I still suggest you to refer the Help File along with my tutorial. You can return to this portion of the page from anywhere down below by hitting the "Home" key on your keyboard.

Expression Statements
Compound Statements
If Statements
Else If Statements
Goto Statements
While Statements
Var Statements
Break and Continue Statements
Halt Statements
Return Statements

Expression Statements
Expression statements are the simplest kind of statements in mIRC. They execute only one command without any evaluation or other complex processings. Examples of expression statements are given below.

alias d run $mircdir
echo -a Hello World!
set %i 10
dialog -x test

Compound Statements
Compound statements are a collection of commands which together define a custom identifier or perform multiple tasks together for an event. They can be piped but are best when contained within curly brackets. Piping is discouraged. Example:

{
    var %a $calc($1 * 5)
    var %b $calc ($2 + 32)
    echo -a $calc(%a + %b)
}

If Statements
If statements are used to evaluate a condition and proceed next accordingly. It is used to make decisions in a program. This statement has two forms. The first is:

    if (condition) {
    command
}


In this form if the condition is true the command is executed else it's not. For example:

on *:text:*:?: { 
  if ($nick isop #channel) { 
    echo -a 14This guys is an OP on #channel 
  }
}
In the above example if the nick which PMed you is an OP on the chanel called #channel, mIRC will echo "This guy is an OP on #channel" in gray (color code 14 in mIRC). If the nick is not an OP on #channel it will do nothing. Now what if you wanted mIRC to echo "This guy is just a regular chatter on #channel" if the nick is not an OP? That's where the second form comes into play - it includes an else. The second form is:

on *:text:*:?: { 
  if (condition) {
    command1 
  }
  else { 
    command2 
  }
} 
We rewrite our code this time using the second form to get the desired result - if the nick is not an OP on #channel mIRC will echo "This guy is just a regular chatter on #channel".

on *:text:*:?: { 
  if ($nick isop #channel) {
    echo -a 14This guys is an OP on #channel 
  }
  else { 
    echo -a 14This guy is just a regular chatter on #channel 
  } 
}
Now that script is certainly not a very useful one. It's only going to annoy you. So you better not use them practically. But it's good enough to test the script and understand the if-else statement.

Before I forget I want to remind you, by condition I don't mean strictly only one condition like if ($nick isop #channel) { command }. You can evaluate two conditions under a condition using the logical AND or logical OR operators. An example:

if (($nick isop #channel) && ($me isvoice #channel)) {
    echo -a $nick is an OP and I am voiced on #channel!
  }
  else {
    echo -a One or both of the conditions are false!
  }
}


An you can also have many independent if statements for an event or an alias. Here's an example:

on *:text:*:?: {
    if ($nick isop #channel) echo -a This guy is an OP on #chanel.
    if ($me isvoice #channel) echo -a I am voiced on #channel.
    if ($day == Sunday) echo -a Today is the day.
    else echo -a Today is not Sunday.
    if ($me isop #channel) echo -a I am an OP on #channel.
}

Else If Statements
Now what if you need to evaluate a nested if-else statement? We use the Else If statement. It's actually a series of if statements in which each if is part of the else clause of the previous statement. The format is:

if (condition1) {
command1
}
else if (condition2) {
command2
}
else if (condition3) {
command3
}
else command4
}


If condition1 is true the script executes command1 and halts, else it checks condition2. If condition2 true it executes command2 and halts, else it checks condition3. If condition3 is true it executes command3 and halts, else (if all conditions fail) it executes command4. The following example is a working example of the Else If statement.

on *:text:*:?: { 
  if ($nick isop #channel) { 
    echo -a This guy is an OP on #chanel. 
  } 
  else if ($me isvoice #channel) { 
    echo -a I am voiced on #channel. 
  } 
  else if ($day == Sunday) { 
    echo -a Today is the day! 
  } 
  else { 
    echo -a None of the conditions are true! 
  } 
}
You can get the same result using the regular If-Else statement. But in a situation like this it's preferable and more legible to use Else If than, writing the statements out in their syntactically equivalent fully nested form:

on *:text:*:?: { 
  if ($nick isop #channel) { 
    echo -a This guy is an OP on #chanel. 
  } 
  else { 
    if ($me isvoice #channel) { 
      echo -a I am voiced on #channel. 
    } 
    else { 
      if ($day == Sunday) { 
        echo -a Today is the day! 
      } 
      else { 
        echo -a None of the conditions are true! 
      } 
    } 
  } 
}

Goto Statement
The goto statement allows execution to jump to an arbitrary labeled point in the script, just as in Perl and C. The labelling is a little different though - the colon prefixes the label. The following example should explain you how the goto statement works.

alias gototest {
    if ($1 == 1) goto one
    else if ($1 == 2) goto two
    else goto unknown

    :one
    echo -a It's one!
    halt

    :two
    echo -a It's two!
    echo -a Spelled T-W-O.
    halt

    :unknown
    echo -a Unkown number!
    halt

}


Don't forget to use the halt at the end of the command(s) under a label or else the next label will be executed too. Applying it's ability to jump anywhere in the code the goto statement can be used to loop too. The example below is one such.

alias test {
    var %i 0
    :again
    if (%i <= 10) {
      echo -a %i
      inc %i
      goto again
    }
  else echo -a Counted from 0 to 10.
}


You could get into an infinite loop if you are not cautious. The mIRC Help File informs "Using a goto incorrectly could lead to an infinite loop. You can break out of a currently running script by pressing Control+Break." If you really need to loop use the While statement instead it's much better. And it's the 'official' loop statement.

While Statement
Just as the If statement is the basic control statement that allows the script to make decisions, the While statement is the basic statement that allows the script to perform repetitive actions. It has the following syntax:

while (condition) {
    command
}


The While statement works by first evaluating the (condition), if it's false it moves on to the next statement in the script else the statement that forms the body of the loop is executed and the (condition) is evaluated again. If it's false the execution goes to the next statement in the script else the body of the loop is executed and the (condition) is evaluated again. It goes on and on this way till the (condition) returns false. The concept will become clear with the following example:

alias whileloop {
    var %i 0
    while (%i <= 10) {
      echo -a %i
      inc %i
    }
  echo -a Counted from 0 to 10.
}


We create an alias named "whileloop". When envoked it first creates a local variable "%i" with the value 0. Now comes the condition part. The condition is while (%i <= 10). Since the value of %i is 0, the condition return true and the body of the loop is executed. It echoes the current value of %i that's 0 and then increases the value of %i by one unit - to 1. The new value of %i (1) is evaluated again to see if it's less than or equal to 10. It returns true, the body of the loop is executed again and the new value of %i is evaluated again. It goes on this way till the value of %i becomes 11 which is more than 10. The condition returns false and the execution moves on to the next statement and echoes "Counted from 0 to 10."

Compare the loop examples for the Goto and While statements. They return the same results. Which one do you prefer to use?

Var Statements
Var statements are covered in the chapter on Variables.

Break and Continue Statements
The Break statement is used to break out of a loop after evaluating a condition under the main condition (the condition which started the loop). Here's an example to demonstrate it's possible use.

alias search { 
  var %i 1 
  var %n $nick($2,0) 
  while (%i <= %n) { 
    var %nick $nick($2, [ %i ] ) 
    echo -a %nick 
    if (%nick == $1) { 
      echo -a -> %nick found on $2 $+ ! 
      break 
    } 
    inc %i 
  } 
  echo -a Search Finished. 
} 
This alias will search for a nick on a channel. The format is /search <nick> <channel>. For it to work you will need to be on that channel of course. Not just to explain the break statement, this script is also a good loop example and also shows the use of an important identifier in mIRC $nick().

The above script is explained here: First a local variable %i with the value of 1 is created. Next another local variable %n is created; it's value is the value returned by the $nick() identifier. We use $nick($2,0) where $2 is the channel name (the second parameter); it returns the number of nicks on the channel.

The while (%i <= %n) condition defines that as long as %i is less than or equal to the number of nicks on the channel execute the body of the loop. The body of the loop: Set a local variable %nick with the value returned by the $nick($1, [ %i ] ) identifier. Since initially the value of %i is 1 the value of the %nick variable is the first nick on the channel. The value of %nick is echoed next. Then comes an If statement which checks for the condition: if the value %nick is equal to the first parameter you supplied to the identifier (the nick you are looking for). If it returns true, the script echoes "<nick> found on <channel>!" and proceeds to the next statement and echoes "Search Complete.". If it returns false the value of %i is increased and the same process continues. It goes on till all the nicks on the channel are checked and finally echoes "Search Complete.".

It's not a very practical script, it was just to demonstrate how the break statement works.

The Continue statement is used go back to the beginning of the loop after evaluating a condition within the body of a loop. An example using the continue statement is shown below. It prints all the numbers from 0 to 10 except 5.

alias no5 { 
  var %i 0
  while (%i <= 10) {
    if (%i == 5) {
      inc %i
      continue
    }
    echo -a The number is: %i
    inc %i
  }
  echo -a -
  echo -a See I didn't echo number 5! 
}

Halt Statements
The Halt statement is used to halt the further processing of a script. It can be used to prevent default responses by mIRC to various event too. Now in the example for the break statement you might have wished the script echoed "<nick> found on <channel> and stopped completely if the nick is on the channel else echo "<nick> not found on <channel>." We can do it with the use of the halt statement.

alias search { 
  var %i 1 
  var %n $nick($2,0) 
  while (%i <= %n) { 
    var %nick $nick($2, [ %i ] ) 
    echo -a %nick 
    if (%nick == $1) { 
      echo -a -> %nick found on $2 $+ ! 
      halt 
    } 
    inc %i 
  } 
  echo -a Search Finished: $1 not found on $2 $+ .
}
Wondering how you halt the default responses of mIRC to vaious events? You suffix the asterix (*) with a caret (^), specify your command and then use the haltdef command. Here's an example:

on *^:join:#: {
  echo -a Hey! $nick has joined $chan $+ .
  haltdef
}


Not just for the ON JOIN event, it can be used for all other mIRC events including CTCP. The only exception is the CTCP VERSION event. You can include your own response to that event but haltdef won't halt mIRC from sending the info about your mIRC version and that Khaled wrote the software.

Return Statements
This statement halts the currently executing routine and allows the calling routine to continue processing. If the routine was not called by any routine the whole script is halted. The return statement is also used to return values from an Alias. That's how we create Custom Identifiers. What is a routine?.

If you are wondering "What exactly is the difference between halt and return?", read on. When you use halt, the processing of the script is stopped then and there. When you use return, the processing of the routine (alias/custom identifier) stops and it returns a value to the calling routine (mIRC command/alias/custom identifier). The return value can be null or non-null. And the calling routine can continue executing. The examples below demonstrate the similarity and difference between halt and return.

alias hnr {
  echo -a The next line will halt the script with the halt command.
  halt
  echo -a This line won't be echoed.
}
Replace halt with return in the above alias, the result is the same. It may seem like halt and return do the same thing. In an isolated alias actually it is so; but when you call an Alias or a Custom Identifier from another alias or a custom identifier using halt instead of return and vice versa makes a difference of day and night.

alias hnr {
  echo -a Calling an alias.
  myAlias
  echo -a Only return could have allowed this!
}

alias myAlias {
  echo -a This is from myAlias!
  halt
}
Replace halt with return in the alias myAlias and execute /hnr to see the difference it makes.

A routine is a chunk of code, usually an alias or a custom identifier. It's the same as module or function or routine in other programming languages. Since Khaled mentioned the term routine in the Help File, we will use routine for mIRC functions or modules. And mIRC routines can either be multi-lined Aliases or Custom Identifiers.

Here's an interesting little alias I wrote: it checks if a number is prime or not. Study the code till you understand, it will be 'enlightening'. The way to execute the alias is /prime <number>. By the way, 31337 is a prime number =:D

alias prime {
  var %i 2
  while (%i < $calc($1-1)) {
    var %n $calc($1 % %i)
    if (%n == 0) {
      echo -a $1 is not a prime number!
      halt
    }
    inc %i
  }
  echo -a $1 is a prime number!
} 

Now that you have learnt about Aliases, Identifiers, Variables, Loops, Conditions, Operators and all, you are ready to move on to the next step - Remotes.


Back | Table of Contents | Remotes



Copyright © 2002-2004 SpyderWares.
Feel free to distribute this tutorial in part or whole, just make sure the credits stay intact.

http://spyderwares.com